package com.chiangleoi.appointment.appointments;


import com.alibaba.fastjson.JSON;
import com.chiangleoi.appointment.constant.DateFormatPattern;
import com.chiangleoi.appointment.model.AppointmentsJson;
import com.chiangleoi.appointment.model.PlaceJson;
import com.chiangleoi.appointment.model.UserJson;
import com.chiangleoi.appointment.model.fileJson.PlacesList;
import com.chiangleoi.appointment.model.fileJson.SitePositionList;
import com.chiangleoi.appointment.model.fileJson.VenueList;
import com.chiangleoi.appointment.utils.BeanCopyUtil;
import com.chiangleoi.appointment.utils.DateUtils;
import com.chiangleoi.appointment.utils.HttpUtils;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 只定周末早上8点的场地
 */
public class Weekday8BadmintonTrain {

    private final static Integer times = 10000000;


    public static void main(String[] args) throws Exception {
        String today = dateToString(new Date());
        String tomorrow = DateUtils.dateAfter(today);

        System.out.println("----------------  预约时间 -----------------");
        System.out.println("----------------------------------------------");
        System.out.println("今日日期为:" + today);
        System.out.println("预约场地时间为:" + tomorrow);

        Path path = Paths.get("src/main/resources/Weekday8TrainBadmintonPlaces.json");
        String fileContent = Files.readString(path);
        PlacesList placesList = JSON.parseObject(fileContent, PlacesList.class);
        List<VenueList> venue = placesList.getResult().getVenue();
        System.out.println("----------------  获取JSON信息成功 -----------------");
        System.out.println("----------------------------------------------");

        // 组装用户信息
        List<UserJson> userJsons = new ArrayList<>();
        users(userJsons);

        System.out.println("----------------  打印预约信息 -----------------");
        System.out.println("----------------------------------------------");
        // 组装场地信息
        List<String> appointmentsJson = places(userJsons, venue, tomorrow);
        System.out.println("----------------------------------------------");
        System.out.println("----------------  信息组装完毕 -----------------");


        System.out.println("----------------------------------------------");
        System.out.println("----------------  打散预约信息 -----------------");
        shuffle(appointmentsJson);
        System.out.println("----------------  打散信息完成 -----------------");
        System.out.println("----------------------------------------------");

        System.out.println("----------------------------------------------");
        System.out.println("----------------  等待输入信息 -----------------");
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();

        System.in.read();


        // 并发执行请求
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        String data = "{\"bookTime\":\"2024-10-26\",\"jsonObject\":{\"id\":\"1781662598041706498\",\"nickName\":\"蒋先生\",\"openId\":\"o21MX4yoIEbMt_qEhOEbIL3e5QTo\"},\"subscribeVos\":[{\"appointments\":\"2024-10-26\",\"ballCourtId\":\"03\",\"id\":193443,\"placeId\":28,\"placeName\":\"八号场\",\"scheduleId\":\"15\",\"sjName\":\"8:00-9:00\"},{\"appointments\":\"2024-10-26\",\"ballCourtId\":\"03\",\"id\":193444,\"placeId\":28,\"placeName\":\"八号场\",\"scheduleId\":\"16\",\"sjName\":\"9:00-10:00\"}]}";

        for (int index = 0; index < times; index++) {
            executorService.submit(new Runnable() {
                public void run() {
                    try {
//                        String data = appointmentsJson.get(0);
                        HttpUtils.doHttp(data);
                        TimeUnit.SECONDS.sleep(1);
                    } catch (Exception e) {

                    }
                }
            });
        }

        executorService.shutdown();
    }


    private static void shuffle(List<String> appointmentsJson) {
        Collections.shuffle(appointmentsJson);
        for (String appointmentJson : appointmentsJson) {
            System.out.println("【预约信息】: " + appointmentJson);
        }
    }

    /**
     * 场地信息
     */
    private static List<String> places(List<UserJson> userJsons, List<VenueList> venue, String tomorrow) {
        List<String> appointmentsJsons = new ArrayList<>();
        for (UserJson userJson : userJsons) {
            for (VenueList venueList : venue) {
                List<SitePositionList> sitePosition = venueList.getSitePosition();
                AppointmentsJson appointmentsJson = new AppointmentsJson();
                appointmentsJson.setJsonObject(userJson);
                appointmentsJson.setBookTime(tomorrow);

                useSitePositionInfo(appointmentsJson, sitePosition, tomorrow);

                String jsonString = JSON.toJSONString(appointmentsJson);
                appointmentsJsons.add(jsonString);

                String nickName = userJson.getNickName();
                String placeName = venueList.getPlaceName();

                System.out.println("nickName: 【" + nickName + "】,场地信息:【" + placeName + "】");
                System.out.println("预约信息: " + jsonString);

            }
        }
        return appointmentsJsons;
    }

    private static void useSitePositionInfo(AppointmentsJson appointmentsJson, List<SitePositionList> sitePosition, String tomorrow) {
        List<PlaceJson> subscribeVos = new ArrayList<>();

        SitePositionList sitePosition1 = sitePosition.get(0);
        SitePositionList sitePosition2 = sitePosition.get(1);

        PlaceJson place1 = BeanCopyUtil.copy(sitePosition1, PlaceJson.class);
        PlaceJson place2 = BeanCopyUtil.copy(sitePosition2, PlaceJson.class);
        place1.setAppointments(tomorrow);
        place2.setAppointments(tomorrow);


        subscribeVos.add(place1);
        subscribeVos.add(place2);

        appointmentsJson.setSubscribeVos(subscribeVos);
    }

    /**
     * 用户信息
     */
    private static void users(List<UserJson> userJsons) {
        userJsons.add(new UserJson("1781662598041706498", "o21MX4yoIEbMt_qEhOEbIL3e5QTo", "蒋先生"));
//        userJsons.add(new UserJson("1842702011991183361", "o21MX4xTGJ9O1WfE7tccPbef6Odc", "芳芳酱"));
//        userJsons.add(new UserJson("1837354252060708865", "o21MX4_pZBZA1L3sORI8BdD4FCk8", "繁星"));

//        userJsons.add(new UserJson("1701245426119802881", "o21MX48B29n6tJyNcG631JMxLTvo", "赵女士"));


    }

    public static String dateToString(Date d) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateFormatPattern.DATE);
        return simpleDateFormat.format(d);
    }

}
